package maMetrics;
import java.util.ArrayList;

import maConfigration.MetricSettings;
import maUtil.LogFile;
import maUtil.RecordUnit;
import maUtil.SortedRecords;

import com.telelogic.rhapsody.core.IRPAttribute;
import com.telelogic.rhapsody.core.IRPClass;
import com.telelogic.rhapsody.core.IRPCollection;
import com.telelogic.rhapsody.core.IRPInterfaceItem;
import com.telelogic.rhapsody.core.IRPModelElement;
import com.telelogic.rhapsody.core.IRPOperation;
import com.telelogic.rhapsody.core.IRPPackage;
import com.telelogic.rhapsody.core.IRPProject;


public class MetricCountNonConstAttr extends MetricObject {
	 
	
	SortedRecords _srProj = null;
	SortedRecords _srPkg = null;
	LogFile _logFile = null;
	
	//global variables for creating csv files
	SortedRecords _srProjCsv = null;
	LogFile _csvLogFileProj = null;
	protected LogFile _logRawData = null;
	
	int _nSum = 0;
//	public MetricCountNonConstAttr()
//	{
//		//TODO get from configuration file
//		_sMetricName = "Metric: Number of Non Constant Attributes";
//		_isApplyToProj = true; //
//		_isApplyToCls = true; //
//		
//		_logFile = new LogFile("MetricNCA.txt");
//		
//		_iWeight = 1;
//	}
	
	public MetricCountNonConstAttr(MetricSettings ms)
	{
		super(ms);
		
		_sMetricName = "Metric: Number of Non Constant Attributes";
		if(_isApplyToPkg)
		{
			_srPkg = new SortedRecords(2000, _sMetricName + " Sorted By Packages");
		}
		if(_isApplyToProj)
		{
			_srProj = new SortedRecords(2000, _sMetricName + " Sorted By Packages");
			_srProjCsv = new SortedRecords(10,"NCA,Class");
		}
		_logFile = new LogFile("MetricNCA.txt");
		
		if(_isLogRawData)
		{
			_logRawData = new LogFile("MetricNCMRawData.csv");
			_logRawData.logLn("Value,Classname");
		}

	}
	
	//gets all attributes from the class
	public static int countNonConstAttr(IRPClass cls)
	{
		int numberNonConstAttr = 0;
		IRPCollection coll = cls.getAttributes();
		numberNonConstAttr = hasNonConstAttr(coll);
		return numberNonConstAttr;
	}
	
	//Gets a collection of attributes and loops through them to check if they are constant or not
	private static int hasNonConstAttr(IRPCollection attrColl)
	{
		int nrNonConstAttr = 0;
		for(int i = 0; i < attrColl.getCount(); i++)
		{
			IRPAttribute attr = (IRPAttribute) attrColl.getItem(i);
			if(attr != null){
				if(attr.getIsConstant() == 0)
				{
					nrNonConstAttr++;
				}
			}
		}
		return nrNonConstAttr;
	}


	@Override
	public int applyInProj(IRPProject proj) {
		/*_csvLogFileProj = new LogFile("MetricNCA"+ proj.getName() +".csv");
		if(!_isApplyToProj)
			return 0;
		if(_srProjCsv.getSize() > 0)
		{
			_csvLogFileProj.logLn("NCA,Class");
			_csvLogFileProj.logPln(_srProjCsv.getAllStringCSV());
		}
		//_csvLogFileProj = new LogFile("MetricDIT"+ proj.getName().toUpperCase() +".csv");
		_srProjCsv = new SortedRecords(10, "NCA,Class");*/
		
		int n = _nSum;
		_nSum = 0;
		return n;
	}

	private void log(String str) {

		_logFile.log(str);
	}

	@Override
	public int applyInPkg(IRPPackage pkg) {
		// TODO Auto-generated method stub
		/*if(!_isApplyToPkg)
			return 0;
		if(_srPkg != null)
		{
			log(_srPkg.getAllString());
		}
		_srPkg = new SortedRecords(20, _sMetricName);*/
		return 0;
	}

	@Override
	public int applyInCls(IRPClass cls) {
		// TODO Auto-generated method stub
		if(_srProj == null)
		{
			_srProj = new SortedRecords(2000, _sMetricName + " " + cls.getProject().getName());
		}
		int n = countNonConstAttr(cls);
		RecordUnit ru = new RecordUnit(n, cls.getFullPathNameIn());
		//if(_isApplyToPkg && n > _srPkg.getMinValue()) 
		//	_srPkg.add(ru);
		if(_isApplyToProj && n > 0 && !_srProj.contains(ru)) //_srProj.getMinValue()) 
			_srProj.add(ru);
		if(_isApplyToProj && n >= _srProjCsv.getMinValue())	
			_srProjCsv.add(new RecordUnit(n, cls.getName()));
		_nSum += n;
		
		if(_isLogRawData && n>0) 
			_logRawData.logLn(n + "," + cls.getFullPathNameIn());
		
		return n;
	}

	@Override
	public int applyInItf(IRPInterfaceItem itf) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int applyInOp(IRPOperation op) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int applyInAttr(IRPAttribute attrb) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		if(_srProj != null) log(_srProj.getAllString());
	}
	
}
